import {Avatar} from "antd";
import React from "react";
import {IUserInfoDto} from "@/api/Types/ObjInterfaces.ts";
import UserApi from "@/api/UserApi.ts";

/**
 * 用户数据展示
 */
class UserInfoViewer {
    private userMap: Map<string, IUserInfoDto>

    private constructor() {
    }

    /**
     * 初始化，通过存有用户id的数组初始化
     * @param userIdList
     */
    public static init = async (userIdList: string[]) => {
        const viewer = new UserInfoViewer();
        viewer.userMap = new Map()
        // 对用户id去重，防止重复请求
        for (let item of userIdList) {
            if (viewer.userMap.get(item) == null) {
                const user = await UserApi.getUserById(item);
                viewer.userMap.set(item, user)
            }
        }
        return viewer
    }

    public getNickNameById = (userId: string) => {
        return this.userMap.get(userId).nickName;
    }

    public show = (userId: string) => {
        const user = this.userMap.get(userId)
        window.notificationApi.open({
            message: (
                <Avatar
                    src={"api/img/" + user.avatar}
                />
            ),
            description: (
                <ul>
                    <li>用户名：{user.nickName}</li>
                    <li>昵称：{user.username}</li>
                </ul>
            ),
            placement: "topRight",
            showProgress: true
        })
    }
}

export default UserInfoViewer